package com.hainatong.shiphelper.module.warehouse.viewmodel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.HttpResult;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.mainhome.entity.WhereComBean;
import com.hainatong.shiphelper.module.warehouse.entity.LinYongInfoBean;
import com.hainatong.shiphelper.module.warehouse.entity.WorkerRecordBean;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.base.PageLiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class LinYongRecordViewModel extends BaseViewModel
{

    private int page;

    private boolean isRefresh = false;

    private boolean isHasNextPage = false;

    private String keyWord;

    private String userUuid;

    public LinYongRecordViewModel(@NonNull Application application)
    {
        super(application);
    }


    private MutableLiveData<PageLiveResult<List<LinYongInfoBean>>> linYongRecordResult = new MutableLiveData<>();

    public MutableLiveData<PageLiveResult<List<LinYongInfoBean>>> getLinYongRecordResult()
    {
        return linYongRecordResult;
    }

    /**
     *  查询 领用人 列表
     * @param userUuid
     */
    private void queryLYRecordListByName(int mPage, String userUuid)
    {
        Map<String,Object> param = new HashMap<String,Object>();

        param.put(Constants.Key.PAGE, mPage);
        param.put( Constants.Key.ROWS, Constants.DEF_PAGE_SIZE);

        List<WhereComBean> whereList = new ArrayList<>();
        whereList.add( new WhereComBean( "employer_id", userUuid ) );
        param.put( "wheres", GsonUtil.obj2json( whereList ));

        ApiService.Builder.getChanBoServer()
                .getLinYongRecordLogByParam(param)
                .compose( RxSchedulersUtils.Obs_io_main() )
                .subscribe(new VMObserver<HttpResult<List<LinYongInfoBean>>>() {
                    @Override
                    public void onStart(Disposable d)
                    {
                        addDisposable( d );
                    }

                    @Override
                    public void onError(int code, String errorMsg)
                    {
                        PageLiveResult<List<LinYongInfoBean>> errorResult = new PageLiveResult<>();
                        errorResult.setSuccess(false);
                        errorResult.setRefresh(isRefresh);
                        errorResult.setMsg(errorMsg);
                        if( !isRefresh )
                        {
                            errorResult.setLoadMoreEmpty(true);
                        }
                        getLinYongRecordResult().postValue( errorResult );
                    }

                    @Override
                    public void onSuccess(HttpResult<List<LinYongInfoBean>> result)
                    {
                        if( result.isOk() )
                        {
                            PageLiveResult<List<LinYongInfoBean>> listResult = new PageLiveResult<>();
                            listResult.setSuccess(true);
                            listResult.setRefresh(isRefresh);
                            if( result.getRows() != null && !result.getRows().isEmpty() )
                            {
                                listResult.setData( result.getRows() );
                                if( result.getRows().size() < Constants.DEF_PAGE_SIZE )
                                {
                                    isHasNextPage = false;
                                }
                                else
                                {
                                    isHasNextPage = true;
                                    page = page + 1;
                                }
                            }
                            getLinYongRecordResult().postValue(listResult);
                        }
                        else
                        {
                            PageLiveResult<List<LinYongInfoBean>> failResult = new PageLiveResult<>();
                            failResult.setSuccess(false);
                            failResult.setRefresh(isRefresh);
                            failResult.setMsg(result.getMsg());
                            if( !isRefresh )
                            {
                                failResult.setLoadMoreEmpty(true);
                            }
                            getLinYongRecordResult().postValue(failResult);
                        }
                    }
                });
    }

    /**
     *  查 用户 记录
     * @param uuid
     */
    public void queryUserLYRecordByUuid( String uuid )
    {
        this.page = 1;
        this.isRefresh = true;
        this.isHasNextPage = true;
        this.userUuid = uuid;
        queryLYRecordListByName(this.page , this.userUuid );
    }

    public void tryRefreshLyRecord()
    {
        queryUserLYRecordByUuid(this.userUuid);
    }

    public void loadMoreRecord()
    {
        if( isHasNextPage )
        {
            this.isRefresh = false;
            queryLYRecordListByName(this.page,this.userUuid);
        }
        else
        {
            PageLiveResult<List<LinYongInfoBean>> emptyResult = new PageLiveResult<>();
            emptyResult.setSuccess(false);
            emptyResult.setLoadMoreEmpty(true);
            emptyResult.setRefresh(false);
            getLinYongRecordResult().postValue(emptyResult);
        }
    }



    private MutableLiveData<LiveResult<List<WorkerRecordBean>>> employerResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<WorkerRecordBean>>> getEmployerResult()
    {
        return employerResult;
    }

    /**
     *  查询 领用人 列表
     * @param name
     */
    public void queryLinYongUserListByName(String name)
    {
        ApiService.Builder.getChanBoServer()
                          .getEmployerListByName(name)
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<List<WorkerRecordBean>>>() {
                              @Override
                              public void onStart(Disposable d)
                              {
                                 addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getEmployerResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<List<WorkerRecordBean>> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getEmployerResult().postValue( new LiveResult<>( true ,result.getData() ) );
                                  }
                                  else
                                  {
                                      getEmployerResult().postValue( new LiveResult<>( false ,result.getMessage() ) );
                                  }
                              }
                          });
    }


}
